//
//  ZLWHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWHomePageController.h"
#import "CK_MenuViewController.h"
#import "ZLWCollectionViewCell.h"
#import "CKCycleView.h"
#import "ZLWHomeDetailController.h"
#import "ZLWRadioController.h"
#import "ZLWProductController.h"
#import "ZLWTopicListController.h"
#import "RadioHomeController.h"
@interface ZLWHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  CollectionVew
 */
@property (nonatomic, strong) UICollectionView * collectionView;
/**
 *  数组属性
 */
@property (nonatomic, strong)NSMutableArray *dataSource;
/**
 *  轮播图属性
 */
@property (nonatomic, strong)CKCycleView *cycleView;
/**
 *  header 属性
 */
@property (nonatomic, strong)LORefresh *headerRefresh;
@end

@implementation ZLWHomePageController
-(void)dealloc
{
    [self.collectionView removeObserver:[self.collectionView gifHeader] forKeyPath:@"contentOffset"];
    [self.collectionView removeObserver:[self.collectionView defaultFooter] forKeyPath:@"contentOffset"];
    [self.collectionView removeObserver:[self.collectionView defaultFooter] forKeyPath:@"contentOffSize"];

}

-(void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [self reloadNetWorking];
}
//网络请求
- (void)reloadNetWorking
{
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日.TODAY",@"电台. RADIO",@"阅读.READING",@"话题.TOPICS",@"订阅.SUBCRITION",@"音乐.MUSIC",@"良品.PRODUCTS",@"设置.SETTING"]mutableCopy];
    //初始化数据源
    self.dataSource = [NSMutableArray new];
    [ZLW_NetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:CK_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                ZLWHomeModel *model = [ZLWHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
        }
        //添加到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData
             ];
            [[self.collectionView gifHeader] endRefreshing];
            [[self.collectionView defaultFooter] endRefreshing];
                self.cycleView.imgNameArray = [@[@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"5.jpeg",@"6.jpeg",@"7.jpeg"] mutableCopy];
        });
    }];
}
//点击菜单项的代理
-(void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title
{
    self.navigationItem.title = [NSString stringWithFormat:@"%@",title];
    [self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {
        case 1:
        {
//            ZLWRadioController *radioVC =[[ZLWRadioController alloc] init];
//            [self.navigationController pushViewController:radioVC animated:YES];
            
            RadioHomeController *radioVC = [[RadioHomeController alloc] init];
            [self.navigationController pushViewController:radioVC animated:YES];

            
        }
            break;
        case 6:
        {
            ZLWProductController *ProductVC =[[ZLWProductController alloc] init];
            [self.navigationController pushViewController:ProductVC animated:YES];
            
        }
            break;
        case 3:
        {
            ZLWTopicListController *TopicListVC =[[ZLWTopicListController alloc] init];
            [self.navigationController pushViewController:TopicListVC animated:YES];
            
        }
            
        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    //创建 collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置 layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height *0.25);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView.backgroundColor = [UIColor greenColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZLWCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //注册头的 cell
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
   self.cycleView = [[CKCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    // Do any additional setup after loading the view.
#pragma amrk    上拉下载 和下拉刷新
    __weak typeof(self)weakSelf = self;
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [weakSelf.dataSource removeAllObjects];
        [weakSelf.collectionView reloadData];
        [weakSelf reloadNetWorking];
    }];
//    self.headerRefresh = [self.collectionView gifHeader];
//    [self.headerRefresh setGifName:@"wangnima"];
    //下拉刷新
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        [weakSelf reloadNetWorking];
    }];
}
/**
 *  点击抽屉按钮
 */
- (IBAction)didClickBarbationAction:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else
    {
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}
#pragma mark  CollectionView的Datasouces
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLWCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark  -UICollractionDelegate
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头轮播图

        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLWHomeDetailController *detailVC = [[ZLWHomeDetailController alloc] init];
    ZLWHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
